package com.design.util.moveBox;
import com.design.config.GameFile;
import com.design.config.MoveBoxConfig;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.HashMap;
public class Picture {
    private static final HashMap<Character,Picture> pictures = new HashMap<>();
    private Image img;
    private final Color color;
    private boolean isFile;
    private final char name;
    public char getName() {
        return name;
    }
    private Picture(char name, Color color){
        this.name = name;
        this.color = color;
    }
    protected Picture(char name, File file, Color color){
        this(name,color);
        this.isFile = file.isFile();
        ImageIcon icon = new ImageIcon(file.getAbsolutePath());
        this.img = icon.getImage();
    }
    private static Picture getPicture(char name,String path,Color color){
        File file = new File(path);
        if(pictures.containsKey(name)){
            return pictures.get(name);
        }
        Picture picture = new Picture(name,file,color);
        pictures.put(name,picture);
        return picture;
    }
    public void fill(Graphics g,int x,int y,int width){
        Color color = g.getColor();
        g.fillRect(x,y,width,width);
        if(this.isFile){
            g.drawImage(this.img,x,y,width, width,null);
            return;
        }
        g.setColor(this.color);
        g.fillRect(x,y,width, width);
        g.setColor(color);
    }
    public static Picture get(char name) {
         switch (name) {
            case MoveBoxConfig.CHAR_BOX :return getBox();
            case MoveBoxConfig.CHAR_BOX_POINT :return  getBoxPoint();
            case MoveBoxConfig.CHAR_WALL :return getWall();
            case MoveBoxConfig.CHAR_GROUND :return getGround();
            case MoveBoxConfig.CHAR_BOX_IN_POINT :return getBoxInPoint();
            case MoveBoxConfig.CHAR_PLAYER :return  getPlayer();
             default :return null;
        }
    }
    public static Picture getPlayer(){
        return getPicture(MoveBoxConfig.CHAR_PLAYER, GameFile.IMG_PLAYER, MoveBoxConfig.COLOR_PLAYER);
    }
    public static Picture getWall(){
        return getPicture(MoveBoxConfig.CHAR_WALL, GameFile.IMG_WALL,MoveBoxConfig.COLOR_WALL);
    }
    public static Picture getBox(){
        return getPicture(MoveBoxConfig.CHAR_BOX, GameFile.IMG_BOX,MoveBoxConfig.COLOR_BOX);
    }
    public static Picture getBoxPoint(){
        return getPicture(MoveBoxConfig.CHAR_BOX_POINT, GameFile.IMG_POINT,MoveBoxConfig.COLOR_BOX_POINT);
    }
    public static Picture getBoxInPoint(){
        return getPicture(MoveBoxConfig.CHAR_BOX_IN_POINT, GameFile.IMG_BOX_IN_POINT,MoveBoxConfig.COLOR_BOX_IN_POINT);
    }
    public static Picture getGround(){
        return getPicture(MoveBoxConfig.CHAR_GROUND, GameFile.IMG_GROUND,MoveBoxConfig.COLOR_GROUND);
    }
}
